package com.hex.ds.hdrs.center.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.hex.bigdata.hbda.base.Page;
import com.hex.ds.hdrs.center.dao.CenterConfDao;
import com.hex.ds.hdrs.center.po.CenterConf;
import com.hex.ds.hdrs.common.catalogue.Catalogue;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 中心定义表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-11-26
 */
@Slf4j
@Service
public class CenterConfService {

    @Autowired
    private CenterConfDao centerConfDao;

    @Value("${logback.home}")
    private String home;

    /**
     * 更新中心 时间
     *
     * @param map
     */
    public void updateCenterConf(Map map) {
        log.debug("更新中心【CenterConfService.updateCenterConf】========");
        map.put("upTs", DateUtil.now());
        this.centerConfDao.updateCenterConf(map);
    }

    /**
     * @Method: otherNotMaster <br>
     * @Param: [map] <br>
     * @Return: void <br>
     * @Description：其他中心下线<br>
     * @Author： gc.zuo<br>
     * @Date： 2022/6/27 16:02 <br>
     * @Version： V1.0<br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void otherNotMaster(Map map) {
        log.debug("更新中心【CenterConfService.updateCenterConf】========");
        map.put("upTs", DateUtil.now());
        this.centerConfDao.otherNotMaster(map);
    }


    /**
     * 查询
     *
     * @param map
     * @return
     */
    public CenterConf qry(Map map) {
        return this.centerConfDao.qry(map);
    }

    /**
     * 查询
     *
     * @return
     */
    public List<CenterConf> qryList(Map map) {
        log.debug("更新中心【CenterConfService.updateCenterConf】========");
        return this.centerConfDao.qryList(map);
    }

    /**
     * @Method: add
     * @Param: [centerConf]
     * @Return: void
     * @Description: 新增中心配置
     * @Author: ji.tang
     * @Date: 2022/11/22 14:55
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void add(CenterConf centerConf) {
        log.info("新增中心【CenterConfService.add】========");
        centerConf.setPkId(BaseUtil.getPkId());
        centerConf.setUpTs(DateUtil.now());
        this.centerConfDao.add(centerConf);
    }

    /**
     * @Method: update
     * @Param: [centerConf]
     * @Return: void
     * @Description: 修改中心配置
     * @Author: ji.tang
     * @Date: 2022/11/22 15:12
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void update(CenterConf centerConf) {
        centerConf.setUpTs(DateUtil.now());
        this.centerConfDao.update(centerConf);
    }

    /**
     * @Method: delete
     * @Param: [list]
     * @Return: void
     * @Description: 批量删除中心配置
     * @Author: ji.tang
     * @Date: 2022/11/22 15:12
     * @Version: V1.0
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void delete(List<String> list) {
        log.info("批量删除中心【CenterConfService.delete】========");
        if (list.size() > 0) {
            for (String pkId : list) {
                this.centerConfDao.delete(pkId);
            }
        }
    }

    /**
     * 分页条件查询中心配置
     *
     * @param centerConf
     * @param page
     * @return
     */
    public List<CenterConf> selectAll(CenterConf centerConf, Page page) {
        log.info("批量删除中心【CenterConfService.delete】========");
        Map map = MapUtil.convertObjToMap(centerConf);
        return this.centerConfDao.selectAll(map, page);
    }

    /**
     * 查询中心配置信息
     *
     * @return
     */
    public CenterConf selectCenterInf() {
        log.info("查询中心配置信息【CenterConfService.selectCenterInf】");
        Map map = new HashMap();
        map.put("isMaster", "0");
        map.put("centerStatus", "run");
        return this.centerConfDao.qry(map);
    }

    public CenterConf initMasterInfo() {
        log.info("初始化中心基本信息【CenterConfService.initMasterInfo】");
        return this.centerConfDao.initMasterInfo(null);
    }

    public Boolean checkCenterData(CenterConf centerConf) {
        log.info("【CenterConfService.checkAppData】参数centerConf: {}", centerConf.toString());
        Map map = MapUtil.convertObjToMap(centerConf);
        CenterConf dbCenterConf = this.centerConfDao.qry(map);
        if (dbCenterConf != null && !dbCenterConf.getPkId().equals(centerConf.getPkId())) {
            return false;
        }
        return true;
    }

    /**
     * @Method: getLogFilePath
     * @Param: []
     * @Return: java.util.List<com.hex.ds.hdrs.common.catalogue.Catalogue>
     * @Description: 获取日志文件目录
     * @Author: ji.tang
     * @Date: 2023/3/22 17:18
     * @Version: V1.0
     */
    public List<Catalogue> getLogFilePath() {
        File logDir = new File(home);
        return getCatalogues(logDir);
    }

    /*
     * @Method: getCatalogues <br>
     * @Param: [logDir] <br>
     * @Return: java.util.List<com.hex.ds.hdrs.common.catalogue.Catalogue> <br>
     * @Description：获取日志目录<br>
     * @Author： wz.li<br>
     * @Date： 2023/3/30 17:10 <br>
     * @Version： V2.0.2<br>
     */
    private List<Catalogue> getCatalogues(File logDir) {
        List<Catalogue> catalogues = new ArrayList<>();
        for (File file : logDir.listFiles()) {
            Catalogue catalogue = new Catalogue(file);
            if (!file.isFile()) {
                catalogue.setChildren(getCatalogues(file));
            }
            catalogues.add(catalogue);
        }
        return catalogues;
    }

    /**
     * @Method: getLogFileInfo
     * @Param: [catalogue]
     * @Return: java.util.List<java.lang.String>
     * @Description: 获取日志文件内容
     * @Author: ji.tang
     * @Date: 2023/3/22 17:17
     * @Version: V1.0
     */
    public List<String> getLogFileInfo(Catalogue catalogue) {
        List<String> logContent = FileUtil.readUtf8Lines(catalogue.getFilePath());
        if (logContent.size() > catalogue.getFileLine()){
            return logContent.subList(logContent.size()-catalogue.getFileLine(), logContent.size());
        }
        return logContent;
    }
}
